<template>
  <div class="box">
    <input type="text" v-model="input1" />
    <h2>Son1 子组件</h2>
    从vuex中获取的值: <label>{{ $store.state.count }}</label>
    <br>
    <button @click="handleAdd">值 + 1</button>
    <button @click="handleFive">值 + 5</button>
    <button @click="changeTitle">改标题</button>
    <button @click="handleAddWithParam(3)">值 + 3</button>
    <button @click="handleAddWithParam(6)">值 + 6</button>
    <button @click="handleAddWithParam(9)">值 + 9</button>
    <button @click="changeTitleWithParam">改标题(输入)</button>
    <button @click="handleChangeActions">一秒后修改为输入框的值</button>

    <hr>
    <div>{{ $store.state.list }}</div>
    <div>直接使用 {{ $store.getters.filterList }}</div>
    <div>mapGetters {{ filterList }}</div>

    <hr>
    <div>测试访问模块中的state - 原生方法</div>
    <div>username: {{ $store.state.user.userInfo.name }}</div>
    <button @click="updateUser">更新个人信息</button>
    <button @click="updateUserAsync">一秒后更新个人信息</button>
    <div>theme: {{ $store.state.setting.theme }}</div>
    <button @click="updateTheme">更新主题色</button>
    <hr>
    <div>测试访问模块中的getter - 原生方法</div>
    <div>{{ $store.getters['user/UpperCaseName'] }}</div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Son1Com',
  data () {
    return {
      input1: ''
    }
  },
  computed: {
    ...mapGetters(['filterList'])
  },
  methods: {
    updateUser () {
      // $store.commit(模块名/mutation名，额外传参)
      const newUser = {
        name: 'ab',
        age: 25
      }
      this.$store.commit('user/setUser', newUser)
    },
    updateUserAsync () {
      // $store.commit(模块名/mutation名，额外传参)
      const newUser = {
        name: 'ab',
        age: 25
      }
      this.$store.dispatch('user/setUserSecond', newUser)
    },
    updateTheme () {
      this.$store.commit('setting/setTheme', 'yellow')
    },
    handleAdd () {
      // 错误代码(vue默认不会监测，监测需要成本)
      // this.$store.state.count++
      // console.log(this.$store.state.count) I
      // *****应该通过 mutation 核心概念，进行修改数据
      this.$store.commit('addCount')
    },
    handleFive () {
      this.$store.commit('addFive')
    },
    changeTitle () {
      this.$store.commit('changeTitle')
    },
    handleAddWithParam (n) {
      this.$store.commit('handleAddWithParam', n)
    },
    changeTitleWithParam () {
      this.$store.commit('changeTitleWithParam', this.input1)
    },
    handleChangeActions () {
      // 调用action
      this.$store.dispatch('changeCountAsync', this.input1)
    }
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}

h2 {
  margin-top: 10px;
}
</style>
